﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using IssueIT.WebUI.Models;

namespace Onesoft.StampShop.Helpers
{
    public class PictureHelper
    {
        public Image ResizeImage(Image fullsizeImage, Size requestedSize, CanvasType canvasType, string backgroundColor)
        {
            var newSize = CalculateSize(fullsizeImage.Size, requestedSize);

            if (fullsizeImage.Size == newSize)
                return fullsizeImage;

            bool enforceSize = (canvasType == CanvasType.Fixed) && requestedSize.Width > 0 && requestedSize.Height > 0;
            Size canvasSize;
            if (enforceSize)
            {
                canvasSize = new Size(requestedSize.Width, requestedSize.Height);
            }
            else
            {
                canvasSize = newSize;
            }

            Bitmap newBitmap = new Bitmap(canvasSize.Width, canvasSize.Height);

            newBitmap.SetResolution(96, 96);
            Graphics grph = Graphics.FromImage(newBitmap);

            if (enforceSize)
            {
                Color color = ColorTranslator.FromHtml(backgroundColor);
                grph.Clear(color);
            }
            grph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grph.SmoothingMode = SmoothingMode.HighQuality;
            grph.CompositingQuality = CompositingQuality.HighQuality;
            grph.PixelOffsetMode = PixelOffsetMode.HighQuality;
            //grph.CompositingMode = CompositingMode.SourceOver;

            int posTop = 0;
            int posLeft = 0;
            if (enforceSize)
            {
                posTop = (canvasSize.Height - newSize.Height) / 2;
                posLeft = (canvasSize.Width - newSize.Width) / 2;
            }

            grph.DrawImage(fullsizeImage, posLeft, posTop, newSize.Width, newSize.Height);
            grph.Dispose();

            return newBitmap;
        }

        private Size GetScaledSize(Size fullSize, double scaleFactor)
        {
            return new Size((int)Math.Round((double)(fullSize.Width * scaleFactor)), (int)Math.Round((double)(fullSize.Height * scaleFactor)));
        }

        private Size CalculateSize(Size fullSize, Size requestedSize)
        {

            if (fullSize.Width <= requestedSize.Width && fullSize.Height <= requestedSize.Height)
                return fullSize;

            if (requestedSize.Width == 0 && requestedSize.Height == 0)
                return fullSize; //throw better??


            try
            {
                Size result = fullSize;
                double scaleFactor;
                bool newSizeFound = false;
                if (requestedSize.Width > 0)
                {
                    scaleFactor = ((double)requestedSize.Width) / ((double)fullSize.Width);
                    result = GetScaledSize(fullSize, scaleFactor);
                    if (requestedSize.Height == 0 || result.Height < requestedSize.Height)
                        newSizeFound = true;
                }

                if (!newSizeFound)
                {
                    scaleFactor = ((double)requestedSize.Height) / ((double)fullSize.Height);
                    result = GetScaledSize(fullSize, scaleFactor);
                }
                return result;
            }
            catch
            {
                throw new Exception("The Image could not be rendered with a new Size");
            }

        }

        public ImageCodecInfo GetEncoderInfo(ImageFormat mimetype)
        {
            ImageCodecInfo[] codecArray = ImageCodecInfo.GetImageEncoders();

            for (int counter = 0; counter <= codecArray.Length; counter++)
            {
                if (Equals(codecArray[counter].FormatID, mimetype.Guid))
                {
                    return codecArray[counter];
                }
            }
            return null;
        }

    }
}

